package com.database.controller;

import com.database.character.Comment;
import com.database.character.Result;
import com.database.service.CommentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
@Api(value = "评论controller",tags = {"评论相关接口"})
public class CommentController {
	@Autowired
	private CommentService commentService;
	/*添加评论*/
	@PostMapping
	@ApiOperation(value = "添加评论",notes = "添加评论",httpMethod = "POST")
	@ApiResponse(code = 200, message = "添加评论成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token",value = "token",required = true,dataTypeClass = String.class,paramType = "header")
	})
	public Result addComment(@RequestBody Comment comment){
		int i = commentService.addComment(comment);
		if(i==0){
			return Result.error("评论失败");
		}
		else{
			return Result.success("评论成功",null);
		}
	}

	/*删除评论*/
	@DeleteMapping("/{postId}")
	@ApiOperation(value = "删除评论",notes = "删除评论",httpMethod = "DELETE")
	@ApiResponse(code = 200, message = "删除评论成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deleteComment(@PathVariable Integer postId){
		int i = commentService.deleteComment(postId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}
	/*查询评论*/
	@GetMapping("/{replyId}/{commentType}")
	@ApiOperation(value = "查询评论",notes = "查询评论",httpMethod = "GET")
	@ApiResponse(code = 200, message = "查询评论成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "replyId",value = "回复id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "commentType",value = "评论类型",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getComment(@PathVariable Integer replyId,@PathVariable Integer commentType){
		List<Comment> commentByID = commentService.getCommentByID(replyId, commentType);
		return Result.success("查询成功",commentByID);
	}

	@GetMapping("/commentId/{commentId}")
	@ApiOperation(value = "根据评论id查询评论",notes = "根据评论id查询评论",httpMethod = "GET")
	@ApiResponse(code = 200, message = "根据评论id查询评论成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "commentId",value = "评论id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getCommentByCommentId(@PathVariable Integer commentId){
		Comment commentByCommentId = commentService.getCommentByCommentId(commentId);
		return Result.success("查询成功",commentByCommentId);
	}

	@DeleteMapping("/deleteId/{commentId}")
	@ApiOperation(value = "根据评论id删除评论",notes = "根据评论id删除评论",httpMethod = "DELETE")
	@ApiResponse(code = 200, message = "根据评论id删除评论成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "commentId",value = "评论id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deleteCommentByCommentId(@PathVariable Integer commentId){
		int i = commentService.deleteCommentByCommentId(commentId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}

	@PutMapping("/update")
	@ApiOperation(value = "更新评论",notes = "更新评论",httpMethod = "PUT")
	@ApiResponse(code = 200, message = "更新评论成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result updateCommentByCommentId(@RequestBody Comment comment){
		int i = commentService.updateCommentByCommentId(comment);
		if(i==0){
			return Result.error("更新失败");
		}
		else{
			return Result.success("更新成功",null);
		}
	}
}
